package com.boot.stomp.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description Stomp接口
 * @date 2020/4/27 14:12
 **/
@Slf4j
@Controller
public class StompServiceExample {

    @Autowired
    SimpMessagingTemplate template;

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description /app/msg
     * @date 2021/2/8 17:41
     **/
    @MessageMapping("/example/msg")
    public String appCommunication(String message, Principal principal) {
        log.info("{} : {}", principal.toString(), message);
        template.convertAndSend("/topic/msg", message);
        template.convertAndSendToUser(principal.getName(), "/msg", message);
        return message;
    }

}
